package com.google.com.p.joat_wordfilter;

import java.util.logging.Logger;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * 
 * JavaPlugin for Bukkit server that filters words and sets them 
 * on fire for cussing
 *
 */
public class WordFilter extends JavaPlugin {

	/**
	 * our log
	 */
	public Logger log = Logger.getLogger("Minecraft");
	
    private final WordFilterPlayerListener playerChatListener = new WordFilterPlayerListener(this);
    
    /**
     * called when this is enabled.  We register our events here
     */
    public void onEnable(){ 
        
    	this.playerChatListener.load();
    	
    	PluginManager pm = this.getServer().getPluginManager();
    	
    	pm.registerEvent(Event.Type.PLAYER_CHAT, playerChatListener, Event.Priority.Normal, this);
    	// in case listener tracks player stats, can clear them on logout
    	pm.registerEvent(Event.Type.PLAYER_QUIT, playerChatListener, Event.Priority.Normal, this);
    	
    	log.info("WordFilter has be initialized!");
    }
     
    /**
     * called when we're unloaded
     */
    public void onDisable(){ 
    	log.info("WordFilter has be stopped.");
     
    }

    /**
     * process our commands, called when the player issues one
     * 
     * @param sender the player
     * @param cmd the command they issued, we look for /wf as registered in plugin.yml
     * @param commandLabel
     * @param args any additional arguments
     * @return true if we processed it
     */
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    	
    	if(cmd.getName().equalsIgnoreCase("wf")){ // If the player typed /basic then do the following...
    		if (args.length > 2)
    		{
    		    if(args[0].equalsIgnoreCase("ignite")){
    		        Player s = (Player)sender;
    		        
    		        Player target = getServer().getPlayer(args[1]); 
    		        if (target != null)
    		        {
    		        	target.setFireTicks(100000);
    		        	s.sendMessage("Burning "+args[1]);
    		        }
    		        else
    		        	s.sendMessage("Player "+args[1]+" not found on server.");
    		        return true;
    		    }
    		}
		    else if ( args.length > 1 )
		    {
		    	if ( args[0].equalsIgnoreCase("add")){
			    	this.playerChatListener.addWord(args[1]);
			    	((Player)sender).sendMessage("Word added");
			    	return true;
		    	}
		    	else if ( args[0].equalsIgnoreCase("del")){
			    	this.playerChatListener.removeWord(args[1]);
			    	((Player)sender).sendMessage("Word removed");
			    	return true;
		    	} 
		    }
    	} //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
    	return false; 
    }


}
